<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Mail;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    //用户添加
  public function getAddress()
  {
    $id = session('id');
    $user = DB::table('useraddress')->where('userid',$id)->get();
    return view('order.address',['user'=>$user]);
  }
  //用户地址插入
  public function insert(Request $request)
  {
    //获取收件人的姓名 电话 id
    $data = $request->only('getman','phone','userid','sheng','shi','xian','jiedao');
    //拼接地址
    $address = $request->input('sheng').$request->input('shi').$request->input('xian').$request->input('jiedao');
    $data['address'] = $address;
    $data['name'] = session('username');
    // dd($data);
    // // dd($request->all());
    $res = DB::table('useraddress')->insert($data);
    if($res){
      return redirect('/order/address')->with('success','添加成功');
    }else{
      return back()->with('error','添加失败');
    }
  }

  //用户地址删除
  public function getDelete($id)
  {
    $res = DB::table('useraddress')->where('id',$id)->delete();
    if($res){
      return redirect('/order/address');
    }else{
      return back()->with('error','删除失败');
    }
  }

  //前台个人中心用户地址修改
  public function getEdit($id)
  {

    $res = DB::table('useraddress')->where('id',$id)->first();
    return view('order.edit',['res'=>$res]);
  }

  //前台个人中心地址更新

  public function getUpdate(Request $request,$id)
  {
    $data = $request->only('getman','phone','sheng','shi','xian','jiedao');
    //拼接地址
    $address = $request->input('sheng').$request->input('shi').$request->input('xian').$request->input('jiedao');
    $data['address'] = $address;
    $data['name'] = session('username');
    $res = DB::table('useraddress')->where('id',$id)->update($data);
    if($res){
      return redirect('/order/address');
    }
  }

  //个人信息
  public function getGeren()
  {
    $id = session('id');
    $res = DB::table('users')->where('id',$id)->first();
    
    return view('order.Geren',['res'=>$res]);
  }

  //修改个人信息

  public function postEditname(Request $request)
  {
    $id = session('id');
    $data = [];
    $da = $request->input('nichen');
    $data['nichen'] = $da;
    // dd($data);
    $res = DB::table('users')->where('id',$id)->update($data);
    if($res){
      return redirect('/order/geren');
    }
  }

  //修改个人信息密码 接受ajax
  public function getEditpassowrd(Request $request)
  {
    // echo '111';
    $id = session('id');
    $pass = $request->input('password');
    $data = DB::table('users')->where('id',$id)->first();
    $password = $data->password;
    // dd($password);
    if (Hash::check($pass, $password)) {
        echo 0; //验证成功可以修改密码
    }else{
        echo 1; //验证失败
    }
  }

  //执行修改个人密码
  public function postUpdatepassowrd(Request $request)
  {
    $id = session('id');
    $pass = $request->input('newpassword');
    $data['password'] = Hash::make($pass);
    // dd($data['password']);
    $res = DB::table('users')->where('id',$id)->update($data);
    if($res){
      return redirect('/order/geren')->with('success');
    }
  }

  //发送邮件验证码
    public function getSendmail(Request $request){
      $str = rand(10000,99999);
      $email = $request->input('email');
      echo $str;
    // dd($email);
      Mail::raw('尊敬的用户你的验证码是'.$str, function ($message)use($email) {
           //设置邮件标题
          $message->subject('哈哈马激活邮件'); 
           //发送给谁 
          $message->to($email); 
          });
     }

  //执行邮件更新
     public function postEmailupdate(Request $request)
     {
        $id = session('id');
        $data = [];
        $email = $request->input('email');
        // dd($email);
        $data['email'] = $email;
        $res = DB::table('users')->where('id',$id)->update($data);
        if($res){
          return redirect('/order/geren');
        }
     }

     //前台用户个人中心
     public function getGerenzx()
     {
      $id = session('id');
      $res  = DB::table('users')->where('id',$id)->first();
      return view('order.gerenzhongxin',['res'=>$res]);
     }

     //前台个人订单
     public function getDdan()
     {
        $id = session('id');
        $res_orders = DB::table('orders')->where('user_id',$id)->get();
        //获取订单表
        foreach ($res_orders as $key => $value) {
          //获取订单id
          $goodsid = $value->id;
          //获取地址id

          $res = DB::table('order_goods')
          ->join('goods','goods.id','=','order_goods.goods_id')
          ->where('order_id',$goodsid)->get();
          $res_orders[$key]->goods = $res;
        }
        // dd($res_orders);
        return view('order.dingdan',['res_orders'=>$res_orders]);
     }



     public function getCcc()
     {
        

        $id = session('id');
        $res_orders = DB::table('orders')->where('user_id',$id)->get();
        //获取订单表
        foreach ($res_orders as $key => $value) {
          //获取订单id
          $goodsid = $value->id;
          //获取地址id
          $res = DB::table('order_goods')->where('order_id',$goodsid)->get();
          $res_orders[$key]->goods = $res;
        }
        dd($res_orders);
        return view('order.dingdan',['res_orders'=>$res_orders]);
     }

     //订单详情
     public function getXq($id)
     {
      // dd($id);
      // echo $id;
      $res = DB::table('orders')
      ->join('order_goods','order_goods.order_id','=','orders.id')
      ->join('goods','goods.id','=','order_goods.goods_id')
      ->join('useraddress','useraddress.id','=','orders.address_id')
      ->join('kucun','kucun.gid','=','goods.id')
      ->where('orders.id',$id)
      ->get();
      $order_code = DB::table('orders')->where('id',$id)->get();
      foreach ($order_code as $key => $value) {
        $code = $value->order_code;
      }
      $add = DB::table('orders')
      ->join('useraddress','useraddress.id','=','orders.address_id')
      ->where('orders.id',$id)
      ->get();
      foreach ($add as $k => $v) {
        // dd($v);
        $ress = $v;
      }
      foreach ($res as $key => $value) {
        $gid = $value->goods_id;
        $oid = $value->order_id;
      }
      return view('order.xq',['res'=>$res,'code'=>$code,'ress'=>$ress,'gid'=>$gid,'oid'=>$oid]);
     }

     //取消订单

     public function getEditdd($id)
     {
      $data = [];
      $data['status'] = 0;
      // dd($data);
      $res = DB::table('orders')->where('id',$id)->update($data);
      if($res){
        return back()->with('success','恭喜你取消成功');
      }else{
        return back()->with('error','取消失败,联系管理员');
      }
     }

     public function postTui(Request $request)
     {
      // $data['status'] = 4;
      $time = date('Y-m-d H:i:s',time());
      $uid = session('id');
      $data = $request->only('gid','order_id');
      $data['time'] = $time;
      $data['uid'] = $uid;
      $data['statuss'] = 1;
      $res = DB::table('orders')->where('order_code',$request->input('code'))->update(['status'=>4]);
      $resgodds = DB::table('regoods')->insert($data);
      if($res && $resgodds){
      return redirect('/personal/replace')->with('success','1');
      }else{
      return redirect('/order/ddan')->with('error','0');
      }
     }
     
     public function postHuan(Request $request)
     {
      $time = date('Y-m-d H:i:s',time());
      $uid = session('id');
      $data = $request->only('gid','order_id');
      $data['uid'] = $uid;
      $data['time'] = $time;
      $data['statuss'] = 0;
      // dd($data);
      $res = DB::table('orders')->where('order_code',$request->input('code'))->update(['status'=>3]);
      $resgodds = DB::table('regoods')->insert($data);
      if($res && $resgodds){
      return redirect('/personal/replace')->with('success','1');
      }else{
      return redirect('/order/ddan')->with('error','0');
      }
     }


}